package com.cci.kangdao.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cci.kangdao.dao.SendSMSLogDao;
import com.cci.kangdao.service.SendSMSLogService;
import com.cci.kangdao.utilTool.ApiErrorCode;

@Service("sendSMSLogService")
public class SendSMSLogServiceImpl implements SendSMSLogService {

	private final static Logger log = Logger.getLogger(SendSMSLogServiceImpl.class);
	
	@Autowired
	private SendSMSLogDao sendSMSLogDao;

	@Override
	public void insertSendSMSLog(int flag,String type,String phone,String message,String code) {
		// TODO Auto-generated method stub
		Map<String,Object> map = new HashMap<>();
		if(flag == 0){
			map.put("sendFalg", flag);
			map.put("sendType", type);
			map.put("sendName", getTypeName(type));
			map.put("phone", phone);
			map.put("message", "");
			map.put("ErrCode", code);
			map.put("ErrMsg", ApiErrorCode.getInstance().getErrorMsg(Integer.valueOf(code)));
		}else{
			map.put("sendFalg", flag);
			map.put("sendType", type);
			map.put("sendName", getTypeName(type));
			map.put("phone", phone);
			map.put("message", message);
			map.put("resultCode", code);
			map.put("resultMsg", SMSEnum.getName(toNum(code))+"");
		}
		sendSMSLogDao.insertSendSMSLog(map);
	}
	
	private static int toNum(String str){
		if(isNumeric(str)){
			return Integer.valueOf(str);
		}else{
			return 99999;
		}
	}
	
	public static boolean isNumeric(String str){
        for(int i=str.length();--i>=0;){
        int chr=str.charAt(i);
        if(chr<48 || chr>57)
            return false;
        }
       return true;
    }    
	
	public String getTypeName(String type){
		if("0".equals(type))
			return "注册App获取验证码";
		if("1".equals(type))
			return "密码找回";
		if("2".equals(type))
			return "手机快捷登录";
		if("3".equals(type))
			return "修改密码";
		if("4".equals(type))
			return "初始化信息";
		if("5".equals(type))
			return "申请角色";
		return "未知";
	}
	
	
}
